package com.innjia.sys.controller;

import com.innjia.base.utils.PageUtils;
import com.innjia.base.utils.Query;
import com.innjia.base.utils.Result;
import com.innjia.sys.entity.ContractInfoEntity;
import com.innjia.sys.entity.UserEntity;
import com.innjia.sys.service.ContractInfoService;
import com.innjia.sys.service.CreditArchivesService;
import com.innjia.sys.service.HouseService;
import com.innjia.sys.service.UserAdminService;
import com.innjia.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 合约订单查询
 *
 * @author innjia
 * @date 2017-11-14 09:56:19
 */
@RestController
@RequestMapping("contractinfo")
public class ContractInfoController extends BaseController {
    @Autowired
    private ContractInfoService contractInfoService;

    @Autowired
    private UserAdminService userService;

    @Resource(name = "creditArchivesServiceAlipay")
    private CreditArchivesService creditArchivesService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //	@RequiresPermissions("contractinfo:list")
    public Result list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ContractInfoEntity> contractInfoList = contractInfoService.queryList(query);
        int total = contractInfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(contractInfoList, total, query.getLimit(), query.getPage());

        return Result.ok().put("page", pageUtil);
    }

    /**
     * 搜索
     */
    @RequestMapping("/searchList")
    //	@RequiresPermissions("contractinfo:list")
    public Result searchList(@RequestParam Map<String, Object> params) {
    	//更新所有已过签约时效的合同状态为已作废
    	Date date = new Date();
        List<ContractInfoEntity> datas = contractInfoService.queryByTenantOrLandlordPhone(null);
        if(datas != null && datas.size() > 0) {
        	datas.forEach(contract -> {
	      		 contract.setContractStatus("2");
	      		 contract.setContractTerminationDate(date);
	      		 contract.setUpdateTime(date);
	            contractInfoService.update(contract);
	      	 });
        }
    	
        //查询列表数据
        Query query = new Query(params);
        int size = contractInfoService.searchCountQueryList(params);
        List<ContractInfoEntity> contractInfoList = contractInfoService.searchQueryList(query);
        int total = contractInfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(contractInfoList, total, query.getLimit(), query.getPage());
        pageUtil.setTotalCount(size);

        return Result.ok().put("page", pageUtil);
    }

    /**
     * 征信档案
     *
     * @param params
     * @return
     */
    @RequestMapping("/searchCredit")
    public Result searchCredit(@RequestParam Map<String, Object> params) {
        UserEntity userEntity = ShiroUtils.getUserEntity();
        Map<String, Object> searchParams = new HashMap<>(2);
        searchParams.put("phone", userEntity.getPhone());
        ContractInfoEntity entity = contractInfoService.searchPhone(searchParams);
        if (null == entity) {
            return Result.ok();
        } else {
            Query query = new Query(params);
            query.put("tenantIdCard", entity.getTenantIdCard());
            query.put("tenantName", entity.getTenantName());
            PageUtils pageUtil = creditArchivesService.queryContract(query);
            return Result.ok().put("page", pageUtil);
        }
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //	@RequiresPermissions("contractinfo:info")
    public Result info(@PathVariable("id") String id) {
        ContractInfoEntity contractInfo = contractInfoService.queryObject(id);
        UserEntity user = userService.queryObject(contractInfo.getContractCreatorId());
        //		if (user !=null){
        //			contractInfo.setContractCreatorName(user.getUserName());
        //		}else {
        //			contractInfo.setContractCreatorName("未创建名字");
        //
        //		}
        return Result.ok().put("contractInfo", contractInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //	@RequiresPermissions("contractinfo:save")
    public Result save(@RequestBody ContractInfoEntity contractInfo) {
        contractInfoService.save(contractInfo);

        return Result.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //	@RequiresPermissions("contractinfo:update")
    public Result update(@RequestBody ContractInfoEntity contractInfo) {
        contractInfoService.update(contractInfo);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //	@RequiresPermissions("contractinfo:delete")
    public Result delete(@RequestBody String[] ids) {
        contractInfoService.deleteBatch(ids);

        return Result.ok();
    }

}
